class GameScene extends Scene {
    constructor(dir) {
        super(dir)
        log('call GameScene')
        this.ball = new Ball(dir, 50, 50)
        this.paddle = new Paddle(dir, 100, 250)
        this.blocks = this.loadLevel(3)
        this.addNode(this.ball)
        this.addNode(this.paddle)
        self = this
        this.enableDrag = false
        //keyborad event
        dir.registerAction('a', function () {
            self.paddle.moveLeft()
        })
        dir.registerAction('d', function () {
            self.paddle.moveRight()
        })
        dir.registerAction('f', function () {
            self.ball.onfire()
        })
        this.eventListener()
        this.enableDebugMode(true)
    }
    loadLevel(n) {
        var blockss = []
        n = n - 1
        var level = levels[n]
        for (var i = 0; i < level.length; i++) {
            var p = level[i]
            var b = new Block(this.dir, p)
            blockss.push(b)
        }
        return blockss
    }
    enableDebugMode(enable) {
        if (!enable) {
            return
        }
        var self = this
        window.addEventListener('keydown', function (event) {
            var k = event.key
            if (k == 'p') {
                // 暂停功能
                window.paused = !window.paused
            } else if ('1234567'.includes(k)) {
                // 为了 debug 临时加的载入关卡功能
                log('qie huan guan ka')
                self.blocks = self.loadLevel(Number(k))
            }
        })
        // 控制速度
        document.querySelector('#id-input-speed').addEventListener('input', function (event) {
            var input = event.target
            // log(event, input.value)
            window.fps = Number(input.value)
        })
    }
    eventListener() {
        // mouse event
        var g = this
        g.dir.canvas.addEventListener('mousedown', function (event) {
            var x = event.offsetX
            var y = event.offsetY
            log(x, y, event)
            // 检查是否点中了 ball
            if (self.ball.hasPoint(x, y)) {
                // 设置拖拽状态
                self.enableDrag = true
            }
        })
        g.dir.canvas.addEventListener('mousemove', function (event) {
            var x = event.offsetX
            var y = event.offsetY
            // log(x, y, 'move')
            if (self.enableDrag) {
                log(x, y, 'drag')
                self.ball.x = x
                self.ball.y = y
            }
        })
        g.dir.canvas.addEventListener('mouseup', function (event) {
            var x = event.offsetX
            var y = event.offsetY
            log(x, y, 'up')
            self.enableDrag = false
        })
    }
    update() {
        if (window.paused) {
            return
        }
        if (this.ball.fire) {
            this.ball.move()
        }
        if (this.paddle.collide(this.ball)) {
            this.ball.rebound()
        }
        if (this.ball.y + this.ball.h > 300) {
            var s = new EndScene(this.dir);
            log('die')
            this.dir.replaceScene(s)
        }
        var len = this.blocks.length - 1
        for (var i = 0; i < this.blocks.length; i++) {
            if (this.ball.collied(this.blocks[i]) && this.blocks[i].alive) {
                log('Collied blocks[', this.blocks[i].id, ']', '  curr size:', window.Sid)
                this.ball.rebound()
                this.blocks[i].killed()
                if (!this.blocks[i].alive) {
                    this.removeNode(this.blocks[i])
                    this.blocks.splice(i, 1)
                }
            }
        }
        super.update()
    }
    draw() {
        super.draw()
        this.dir.ctx.fillStyle = "#554"
        this.dir.ctx.fillRect(0, 0, 400, 300)
        for (var i = 0; i < this.blocks.length; i++) {
            this.dir.drawNode(this.blocks[i])
        }
        super.draw()
    }

}